Demo: Correlation and Linear Regression (Self-Paced)

In this demonstration, you explore relationships between numeric predictors and the response variable Donation_Amt using correlation analysis in a SAS Studio flow. Then the input GiftAvgCard36, which is most correlated with the target, is used in a simple linear regression.

Reminder: If you restarted your SAS session, or it has timed out due to inactivity, you must re-create the course libraries, LOCALLIB and VST. To do this, run the AssignLibrary flow.

  1. In SAS Viya, launch SAS Studio by clicking the Applications menu button (the three-by-three grid in the upper left corner), expand ANALYTICS LIFE CYCLE, and click Develop Code and Flows.

  2. In the navigation panel on the far left, click the Explorer button (second from top). Expand Files >Home > workshop > VST. Double-click AssignLibrary.flw to open it.

  3. Click the Run button on the flow toolbar.

Demo Steps

Correlation analysis

  1. In the work area, click the plus sign next to the open tabs to create a new tab. From the drop-down menu, choose Flow. Alternatively, select Flow from the New menu at the top left of SAS Studio.

  2. Click on the Steps icon on the left side of the Navigation Pane. Under SAS Steps tab, expand Data (Input and Output) group, double click on the Table step.

  3. In the upper half of the work area, click on the Table step. In the lower half of the work area, under Table Properties tab, click the file cabinet next to the Library field. Under Libraries on the Left, click on the VST library, then select the PVA_DONORS data on the right and click OK.

  4. Scroll down through SAS Steps, expand Statistics group, then double click on Correlation Analysis.

  5. Next, move your mouse towards the edge of the PVA_DONORS node until the pointer becomes a hand icon. Click and drag an arrow from PVA_DONORS node to the box on the left side of the Correlation Analysis node (the input port). Now Correlation Analysis is connected to the PVA_DONORS data. Click the Arrange nodes button to visually organize the nodes within the flow.

  6. Click on the Correlation Analysis node. In the lower half of the work area, under the Data tab, find the Analysis variables field and click the plus sign to add variables. Add all variables except Donation_Amt and StatusCatStarAll. Do this by clicking the Select all box next to Name and then deselecting Donation_Amt and StatusCatStarAll. Click OK.

  7. Find the Correlate with field. Use the plus sign to add the variable Donation_Amt. Click OK.

  8. Switch tabs to Options. Under Statistics > Statistics to display, select Selected statistics from the drop-down menu. Check the boxes for Display p-values and Order correlations from highest to lowest (in absolute value).

  9. Scroll down and expand Plots. Under Type of plot, select Individual scatter plots from the drop-down menu. Check the Include ellipse box and use the default settings of Prediction Ellipse for the ellipse and 95% for the confidence level.

  10. Uncheck the Include inset statistics box.

  11. Keep Number of Variables to plot set to 5 and change Maximum number of plot points to No limit.

  12. Click Run to execute the flow. Click on the Submitted Code and Results tab at the top. The results appear in the work area. To increase the size of the work area, locate the Correlation Analysis step settings in the lower half of the work area. Click the Minimize preview button (a minus sign icon) at the top right corner of this section.  Alternatively, you can click on More options > Open in a browser tab > Results to open the results in a new browser tab.
An image of the Results tab.

Note: If you see a warning message in the results window that says The maximum size limit has been exceeded, you might want to click the Open in new tab link to view the results. Alternatively, go to the SAS Studio Options menu, and select Preferences. Under SAS Programs, click Results. Under Display warning if results are larger than specific size, change the Maximum result size (MB) from 3 to 10.

Each cell in the Correlation table lists several pieces of information: the variable correlated with the response Donation_Amt, the estimate of the correlation coefficient, the p-value, and the sample size. The p-value is for a test of the null hypothesis that the population correlation equals zero. The sample size depends on the number of nonmissing values for only the two variables being correlated. Because of this, the sample size can differ for each correlation.

The correlations have been ranked from strongest to weakest. The strongest correlation with the target is approximately 0.65 for GiftAvgCard36. This positive correlation indicates that as GiftAvgCard36 increases, Donation_Amt tends to increase. It will likely be a good predictor in a regression model for Donation_Amt. The weakest correlation with the target is -0.006 for PromCntCard12. This negative correlation is not statistically different from zero at any commonly used alpha level.

An image of the Scatterplot with 95% Prediction Eclipse.
An image of the Scatterplot with 95% Prediction Eclipse.
An image of the Scatterplot with 95% Prediction Eclipse.
An image of the Scatterplot with 95% Prediction Eclipse.
An image of the Scatterplot with 95% Prediction Eclipse.

Correlations measure the linear associations between variables. Scatter plots can be useful for assessing linear relationships and thus the appropriateness of correlation analysis. For large data sets, scatter plots can be challenging to interpret when many dots fall on top of each other and are indistinguishable. Prediction ellipses can help you visualize the strength and direction of a correlation. The plots show 95% prediction ellipses for 5 of the 20 variable pairs. The 95% prediction ellipses show where 95% of the data points are expected to lie if both variables are normally distributed. The sign of the correlation becomes obvious from the orientation of the ellipse. The narrower the ellipse, the stronger the linear association between the variables. None of the five plots show strong nonlinear relationships, indicating that correlation is a reasonable tool for describing relationships between these variables. Some plots show unusual data points such as the plot showing Gift Amount Last. Unusual data points, often called outliers, are discussed in a later lesson.

Simple linear regression

Next, we will use a regression node in a flow to regress Donation_Amt onto GiftAvgCard36, the most correlated variable identified earlier in this demonstration. This will produce parameter estimates and hypothesis tests that can be used to understand relationship between variables, as well as a regression equation that can be used to predict future donations from lapsing donors.

  1. Return to the previous flow containing the Correlation Analysis step. Make sure the Flow tab is selected and active before proceeding further. From the Steps menu on the far left pane, ensure that you are on the SAS Steps tab. Expand the Statistics group, then double click on the Linear Regression step to add it to the existing flow. Connect the PVA_DONORS node to the Linear Regression node.

  2. An image of the PVA_DONORS node.
  3. Click on the Linear Regression node. In the lower half of the work area, ensure the Data tab is selected. Locate the Dependent variable field, then click the plus (+) sign and add the variable Donation_Amt. Click OK.

  4. Next, find the Explanatory variables field. Use the plus sign to add GiftAvgCard36.

  5. Continue working into the lower half of the work area to configure the remaining settings for the analysis. Click on the Model tab. Leave the default option for Model selection method (Full model fitted (no selection)) and leave the Include intercept option selected.

  6. Click on the Statistics tab, under Details on Estimates, check the box for Confidence limits for parameter estimates. Use the default value of 95% for Confidence level.

  7. Click on the Plots tab. Under Plots to display, choose Selected plots. Check the boxes next to Observed by predicted values plot, Diagnostic plots, and Scatter plot with regression line.

  8. Right click on the Linear Regression node and choose Run node.

    Note: You can run a subset of the flow by using the following options:

    • Run node - runs only the currently selected node in the flow.

    • Run from node - runs the currently selected node and all nodes that occur downstream from the currently selected node.

    • Run to node - runs all nodes that occur upstream from the currently selected node. The currently selected node is not run.

  9. Save the flow to the VST folder as Correlation and Regression.flw.

  10. Click the Minimize Preview button (minus sign icon) located at the top-right corner of the lower half of the work area to collapse the section and then view the results.
An image of the Model: Linear_Regression Model Results.

In the simple linear regression model, Donation_Amt is regressed on GiftAvgCard36. Only 4111 observations are used. We learned in an earlier lesson that Donation_Amt has missing values for approximately half the observations. Any observation with missing values for the predictor or response is dropped from the analysis.

An image of the Analysis of Variance.

The Analysis of Variance table shows that the model is significant with P < 0.0001. The R-square indicates that about 42% of the variability in Donation_Amt can be explained by GiftAvgCard36. The adjusted R-square equals 0.42, and the mean square error (MSE) is approximately 392. MSE, also called the error variance, is a measure of the variance in the response variable that is not explained by this model. Both the adjusted R-square and MSE can be used for model comparisons, with higher adjusted R-square or lower MSE indicating a better model.

An image of the Parameters Estimates.

The Parameter Estimates table shows that the estimated intercept equals 75.41694 and the estimated slope for GiftAvgCard36 equals 1.79672. Thus, the regression model for estimated donation amount is

An image of the regression model.

The p-value for the GiftAvgCard36 parameter indicates that the population slope is significantly different from zero. Thus, this predictor can be useful to predict future donation amounts. The slope of 1.80 indicates that for every additional dollar of GiftAvgCard36, the predicted donation amount will increase by $1.80. The 95% confidence interval on the slope for GiftAvgCard36 indicates the parameter estimate is likely between (1.73, 1.86). The intercept is also significantly different than zero, but tests of the intercept are often less relevant than tests of the slope. In this case, the intercept tells us that the average donation equals $75.42 when GiftAvgCard36 equals zero.

An image of the Observed by Predicted for Donation_Amt.

The plot of Observed by Predicted values of Donation_Amt shows that predictions match observed donations poorly when observed donations are under $100.

An image of Fit Diagnostics for Donation_Amt.

Several diagnostic plots based on residuals (the difference between observed and predicted values of the response) are shown. These can be used to assess whether the model assumptions are met and to detect extreme observations. Both of these topics are discussed later in this lesson.

An image of the Fit Plot for Donation_Amt.

The Fit plot shows Donation_Amt plotted against GiftAvgCard36. The best-fit regression line (the solid black line) indicates the predicted value of Donation_Amt for specific values of GiftAvgCard36. The thin light gray shaded region around the regression line indicates the 95% confidence interval on the mean response for specific values of the predictor. It is difficult to see here in the picture Fit Plot, but the confidence intervals are narrowest near the mean values of X and Y and widest at the extremes. The dashed lines show the 95% prediction limits. You can be 95% confident that new observations will lie within these prediction limits.